<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
require_once "functions.php";
if(isset($_SESSION['lang']))
    require_once "./language_files/incomingRequests_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/incomingRequests_en.php";

if(!isset($_SESSION['member_id']))
    echo"<script type='text/javascript'>top.window.location='index.php';</script>";
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link rel="stylesheet" type="text/css" href="style3.css"/>
        <script type="text/javascript" src="functions.js"></script>
        <title></title>
    </head>
    <body>
        <?php
        print "<h1>" . $lang['title_1'] . "</h1>";
        echo "<h2 style='text-align:left;'>" . $lang['title_2'] . "</h2>";
        echo "<p align='center'><label style='text-align=center;' id='Validate2'></label></p>";
        //$member_id= $_SESSION['member_id'];
        if(isset($_SESSION['member_id']))
        {  // checks that the current user id is passed in the session
            $member_id=$_SESSION['member_id']; // saves its value in $currentUserID
            // retrieve the invitation request text from the 'request' table to show it to the current user in his/her requests section
            $get_request=mysql_query("SELECT r.request, m.request_id, m.type, m.member_id2  from member_request_member m
            INNER JOIN request r ON (m.request_id = r.request_id) WHERE m.member_id2 = {$member_id} AND m.approve is NULL")
                    or die(mysql_error());
            // count the number of rows returned from the query
            print '<table width="100%">';
            echo "<tr> <td style='color:snow; background-color:#003366; text-align:center; border-radius:6px;'>" . $lang['description'] . "</td>
                        <td style='color:snow; background-color:#003366; text-align:center; border-radius:6px;'>" . $lang['accept'] . "</td>
                        <td style='color:snow; background-color:#003366; text-align:center; border-radius:6px;'>" . $lang['reject'] . "</td>
                        </tr>";
            if(mysql_num_rows($get_request) > 0)
            {


                // initialize transparent table to print in
                // returns the current conference title, from the conference id
                //loop on the results from the query
                while($row=mysql_fetch_assoc($get_request))
                {
                    // check that the request type is for a reviewer, organizer or an author
                    if($row['type'] == 'R' || $row['type'] == 'A' || $row['type'] == 'O')
                    {
                        // save this request id in a variable
                        $request_id=$row['request_id'];

                        // print each request message in the table
                        print " <tr><td>{$row['request']} </td>";
                        $get_conference_id=mysql_query("SELECT conference_id,type from member_request_member where member_id2 ='$member_id' AND
                request_id ='$request_id' AND approve is NULL") or die(mysql_error());
                        $row=mysql_fetch_assoc($get_conference_id);
                        // save the conference id in a variable
                        $confid=$row['conference_id'];
                        $request_type=$row['type'];
                        ?>
                        <td><input requestType="<?php echo $request_type?>" requestID="<?php echo $request_id?>"
                                   confID="<?php echo $confid?>"
                                   member_id="<?php echo $member_id?>" type="button" class="yes1" name="yes" value="<?php echo $lang['accept'];?>"></td>
                        <td><input requestType="<?php echo $request_type?>" requestID="<?php echo $request_id?>"
                                   confID="<?php echo $confid?>"
                                   member_id="<?php echo $member_id?>" type="button" class="no1" name="no" value="<?php echo $lang['reject'];?>"></td></tr>
                            <?php
                        }
                        // incase the request sent was not with the 3 specified types (O,R and A)
                        else
                        {
                            echo $lang['error_1'];
                        }
                    }
                    // print the whole table of pending requests
                }
                else
                {
                    echo "<tr><td colspan='3' style='text-align:center;'>" . $lang['no_request_1'] . "</td></tr>";
                }
            }
            echo '</table>';
            echo "<br />";
            echo "<br />";
            echo "<br />";
//establish the connection with the database
            echo "<h2 style='text-align:left'>" . $lang['title_3'] . "</h2>";
            echo "<p align='center'><label style='text-align=center;' id='Validate'></label></p>";
            print '<table width="100%">';
            echo '
                <tr>
                    <td style="color:snow; background-color:#003366; text-align:center; border-radius:6px;"><b>' . $lang['description'] . '</b></td>
                    <td style="color:snow; background-color:#003366; text-align:center; border-radius:6px;"><b>' . $lang['accept'] . '</b></td>
                    <td style="color:snow; background-color:#003366; text-align:center; border-radius:6px;"><b>' . $lang['reject'] . '</b></td>
                </tr>';
           
//take the current member id from the s
            $id=$_SESSION['member_id'];
//check whether this user is main organizer or not
            $check_Mainorganizer=mysql_query("SELECT * FROM conference
                       WHERE creator_id ='$id'") or die(mysql_error());
//enter the while loop only if the current user is a main organizer in one of the conferences
            $num_row=0;
            While($row=mysql_fetch_assoc($check_Mainorganizer))
            {
                // take the conference id and the name of the conference
                $conference_id=$row['conference_id'];
                $conference_name=formatText_safe($row['long_name']);

//check whether this conference has any requests or not
                $has_Request=mysql_query("SELECT * FROM  member_subscribesto_conference
                       WHERE conference_id ='$conference_id' AND approve is NULL") or die(mysql_error());
//if has_request is empty then he has no requests
// if the query return some value

                if(mysql_num_rows($has_Request))
                {
                    // initialize transparent table to print in
                    //enters the while only if there is at least one request
                    while($row=mysql_fetch_assoc($has_Request))
                    {
                        //take the request id and type from table Member_SubscribesTo_Conference
                        $request_id=$row['member_id'];
                        $request_type=$row['request_type'];
                        //check the requestor name from table members
                        $requestor_name=mysql_query("SELECT * FROM member
                       WHERE member_id ='$request_id'") or die(mysql_error());
                        $rowName=mysql_fetch_assoc($requestor_name);
                        $request_name=$rowName['first_name']." ".$rowName['last_name'];
                        $num_row=mysql_num_rows($has_Request);
                        // if the requestor wants to become an organizer
                        if($request_type == '2')
                        {
                            print " <tr><td> $request_name" ." ". $lang['organizer_request'] . " $conference_name </td> ";
                        } //if he wants to become a reviewer
                        else if($request_type == '1')
                        {
                            print "<tr><td> $request_name" ." ". $lang['reviewer_request'] . " $conference_name  </td>";
                        }
                        else if($request_type == '3')
                        {
                            print "<tr><td> $request_name " ." ". $lang['author_request'] . " $conference_name  </td>";
                        }
                        ?>

                    <td><input requestType="<?php echo $request_type?>" requestID="<?php echo $request_id?>"
                               confID="<?php echo $conference_id?>" confName="<?php echo $conference_name?>"
                               member_id="<?php echo $id?>" type="button" class="yes" name="yes" value="<?php echo $lang['accept'];?>"></td>
                    <td><input requestType="<?php echo $request_type?>" requestID="<?php echo $request_id?>"
                               confID="<?php echo $conference_id?>" confName="<?php echo $conference_name?>"
                               member_id="<?php echo $id?>" type="button" class="no" name="no" value="<?php echo $lang['reject'];?>"></td></tr>

            <?php
        }
    }
}
if($num_row == 0)
{

    echo "<tr><td colspan='3' style='text-align:center;'>" . $lang['no_request_2'] . "</td></tr>";
}
echo "</table>";
?>
<?php require_once "jquery.php";?>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
                label = top.document.getElementById("num_requests");
                $.ajax ({
                    type: 'POST',
                    url:  'num_requests.php',
                    success: function(response) {
                        if(response == "0") {
                            $(label).html('');
                            $(label).removeClass("num_requests");
                        } else {
                            $(label).text(response);
                            $(label).addClass("num_requests");
                        }
                    }
                });
                $(".yes").bind("click", function()
                {
                    rt = $(this).attr("requestType");
                    rID = $(this).attr("requestID");
                    mID = $(this).attr("member_id");
                    cN = $(this).attr("confName");
                    cID = $(this).attr("confID");
                    ty = $(this).attr("value");
                    $.ajax ({
                        data: {requestType:rt, requestID: rID,
                            member_id: mID,
                            confName: cN, confID: cID, type:ty},
                        type: 'POST',
                        url:  'respondtoRequest.php',
                        success: function(response) {
                            if(response == 1) {
                                main_iframe=top.document.getElementById("collabsoft");
                                $(main_iframe).attr("src", $(main_iframe).attr("src"));
                                alert("request has been accepted and message is sent to member to inform him");


                            } else {
                                alert(response);
                            }
                        }
                    });
                });
                $(".no").bind("click",function()
                {
                    rt = $(this).attr("requestType");
                    rID = $(this).attr("requestID");
                    mID = $(this).attr("member_id");
                    cN = $(this).attr("confName");
                    cID = $(this).attr("confID");
                    ty = $(this).attr("value");
                    $.ajax ({
                        data: {requestType:rt, requestID: rID,
                            member_id: mID,
                            confID: cID, type:ty, confName: cN},
                        type: 'POST',
                        url:  'respondtoRequest.php',
                        success: function(response) {
                            if(response == 1) {
                                main_iframe=top.document.getElementById("collabsoft");
                                $(main_iframe).attr("src", $(main_iframe).attr("src"));
                                alert("request has been rejected and message is sent to member to inform him");
                            }
                            else {
                                alert(response);
                            }
                        }
                    });
                });
                $(".yes1").bind("click",function(){
                    rt = $(this).attr("requestType");
                    rID = $(this).attr("requestID");
                    mID = $(this).attr("member_id");
                    cID = $(this).attr("confID");
                    ty = $(this).attr("class");
                    $.ajax ({
                        data: {requestType:rt, requestID: rID,
                            member_id: mID,
                            confID: cID, type2:ty},
                        type: 'POST',
                        url:  'respondtoRequest.php',
                        success: function(response) {
                            if(response == 1)
                            {
                                main_iframe=top.document.getElementById("collabsoft");
                                $(main_iframe).attr("src", $(main_iframe).attr("src"));
                                alert("invitation has been accepted");
                            }
                            else
                            {
                                alert(response);
                            }
                        }
                    });
                });
                $(".no1").bind("click",function()
                {
                    rt = $(this).attr("requestType");
                    rID = $(this).attr("requestID");
                    mID = $(this).attr("member_id");
                    cN = $(this).attr("confName");
                    cID = $(this).attr("confID");
                    ty = $(this).attr("class");

                    $.ajax ({
                        data: {requestType:rt, requestID: rID,
                            member_id: mID,
                            confName: cN, confID: cID, type2:ty},
                        type: 'POST',
                        url:  'respondtoRequest.php',
                        success: function(response) {
                            if (response == 1) {
                                main_iframe=top.document.getElementById("collabsoft");
                                $(main_iframe).attr("src", $(main_iframe).attr("src"));
                                alert("invitation has been rejected");

                            } else {
                                alert(response);
                            }
                        }
                    });
                });
            });
            function error2(text) {
                document.getElementById("Validate2").style.color = "green";
                document.getElementById("Validate2").innerHTML= text;
            }
            function error(text) {
                document.getElementById("Validate").style.color = "green";
                document.getElementById("Validate").innerHTML= text;
            }

        </script>
    </body>
</html>